package com.infra.micro.id.controller;

import com.infra.common.resp.PageEx;
import com.infra.common.resp.Response;
import com.infra.micro.id.domain.entity.IdAppPO;
import com.infra.micro.id.domain.service.IdAppService;
import com.infra.micro.id.model.req.QueryIdTokenByCondReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/id/token")
@Api(tags = {"应用管理api服务"})
public class IdAppController {

    @Resource
    private IdAppService idAppService;

    @ApiImplicitParam(name = "appId", value = "应用ID", dataType = "Long", dataTypeClass = Long.class, paramType = "path", required = true)
    @ApiOperation(value = "通过应用ID查详情", notes = "通过应用ID查详情", produces = "application/json")
    @GetMapping("/{appId}")
    public Response<IdAppPO> queryByAppId(@PathVariable("appId") Integer appId) {
        return idAppService.queryByAppId(appId);
    }

    @ApiImplicitParam(name = "appId", value = "应用ID", dataType = "Long", dataTypeClass = Long.class, paramType = "path", required = true)
    @ApiOperation(value = "通过应用ID更新详情", notes = "通过应用ID更新详情", produces = "application/json")
    @PutMapping("/{appId}")
    public Response<Integer> update(@PathVariable("appId") Integer appId, IdAppPO record) {
        return idAppService.update(appId, record);
    }

    @ApiOperation(value = "新增应用令牌信息", notes = "新增应用令牌信息", produces = "application/json")
    @PostMapping("")
    public Response<Integer> insert(@RequestBody IdAppPO record) {
        return idAppService.insert(record);
    }

    @ApiOperation(value = "查应用令牌配置列表(分页)", notes = "查应用令牌配置列表(分页)", produces = "application/json")
    @GetMapping("/list")
    public Response<PageEx<IdAppPO, QueryIdTokenByCondReq>> queryByPage(QueryIdTokenByCondReq req) {
        return idAppService.queryByPage(req);
    }

    @ApiOperation(value = "刷新应用缓存", notes = "刷新应用缓存", produces = "application/json")
    @GetMapping("/cache/refresh")
    public Response<Boolean> refreshCache() {
        return idAppService.refreshCache();
    }

    @ApiOperation(value = "获得新的密钥串", notes = "获得新的密钥串（签名模式）", produces = "application/json")
    @GetMapping("/secret/key")
    public Response<String> generateSecretKey() throws Exception {
        return idAppService.generateSecretKey();
    }
}
